<!--Copyright (C) Kherty.  All rights reserved.-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="OpenLS.Spreadsheet.UI.WorkbookResources"
                    xmlns:ss="http://schemas.Kherty.com/office/2007/spreadsheet"
                    xmlns:drawingui="http://schemas.Kherty.com/office/2007/drawing"
                    xmlns:oui="http://schemas.Kherty.com/office/2007" xmlns:ssui="clr-namespace:OpenLS.Spreadsheet.UI"
                    xmlns:Internals="clr-namespace:OpenLS.Spreadsheet.UI.Internals">
  <ResourceDictionary.MergedDictionaries>
    <oui:ButtonResources></oui:ButtonResources>
    <oui:ScrollBarResources />
    <ssui:WorksheetTabResources />
    <Internals:EditedCellResources />
    <ssui:WorksheetResources />
    <ssui:ChartSheetResources />
    <ssui:SpreadsheetDocumentResources />
    <ssui:DragDropListBoxResources />
    <Internals:FormulaBarResources />
  </ResourceDictionary.MergedDictionaries>

  <DataTemplate DataType="{x:Type ss:Workbook}" x:Key="smallWorkbook">
    <Grid Grid.Row="1" ssui:SpreadsheetElement.Workbook="{Binding}" Background="White"
          ssui:SpreadsheetElement.WorksheetViewModel="{Binding View.ActiveSheet, Converter={x:Static Internals:SpreadsheetConverters.ViewModelFactory}}">
      <Grid.CommandBindings>
        <CommandBinding Command="{x:Static ssui:SpreadsheetCommands.InsertWorksheet}"
                        Executed="OnInsertWorksheet" CanExecute="OnIsNotProtected" />
        <CommandBinding Command="{x:Static ssui:SpreadsheetCommands.HideSheet}" Executed="OnHideWorksheet"
                        CanExecute="OnCanHideWorksheet" />
        <CommandBinding Command="{x:Static ssui:SpreadsheetCommands.UnhideSheet}" Executed="OnUnhide"
                        CanExecute="OnCanUnhide" />
        <CommandBinding Command="{x:Static ssui:SpreadsheetCommands.MoveTo}" Executed="OnMoveTo"
                        CanExecute="OnAlways" />
        <CommandBinding Command="{x:Static ssui:SpreadsheetCommands.MoveOrCopySheet}"
                        Executed='OnMoveOrCopySheet' CanExecute="OnIsNotProtected" />
        <CommandBinding Command="{x:Static ssui:WorkbookNavigationCommands.MoveToFirst}"
                        Executed="OnMoveToFirst" CanExecute="OnAlways" />
        <CommandBinding Command="{x:Static ssui:WorkbookNavigationCommands.MoveToLast}" Executed="OnMoveToLast"
                        CanExecute="OnAlways" />
        <CommandBinding Command="{x:Static ssui:WorkbookNavigationCommands.MoveToPrevious}"
                        Executed="OnMoveToPrevious" CanExecute="OnAlways" />
        <CommandBinding Command="{x:Static ssui:WorkbookNavigationCommands.MoveToNext}" Executed="OnMoveToNext"
                        CanExecute="OnAlways" />
        <CommandBinding Command="{x:Static ssui:WorkbookNavigationCommands.MoveTo}"
                        Executed="OnMoveToWorksheet" CanExecute="OnAlways" />
      </Grid.CommandBindings>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Rectangle Fill="{DynamicResource {x:Static ss:SpreadsheetBrushes.SheetBackgroundKey}}" Grid.Column="4"
                 Grid.Row="1" />
      <DockPanel Grid.Row="1" Name="navigationBox"
                 Internals:FocusElement.FocusRedirect="{Binding ElementName=activeSheet}">
        <DockPanel.Resources>
          <Style TargetType="{x:Type Path}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Fill" Value="{DynamicResource {x:Static oui:OfficeBrushes.TextForegroundKey}}" />
            <Setter Property="Stroke"
                    Value="{DynamicResource {x:Static oui:OfficeBrushes.TextForegroundKey}}" />
            <Setter Property="StrokeThickness" Value="1" />
          </Style>
        </DockPanel.Resources>
        <Button DockPanel.Dock="Left" Style="{StaticResource sheetNavigationButton}"
                Command="{x:Static ssui:WorkbookNavigationCommands.MoveToFirst}" CommandTarget="{Binding ElementName=lb}">
          <Path Data="M0,0 L0,6 M6,0 L3,3 L6,6 L6,6" />
        </Button>
        <Button DockPanel.Dock="Left" Style="{StaticResource sheetNavigationButton}"
                Command="{x:Static ssui:WorkbookNavigationCommands.MoveToPrevious}" CommandTarget="{Binding ElementName=lb}">
          <Path Data="M0,0 M6,0 L3,3 L6,6" />
        </Button>
        <Button DockPanel.Dock="Left" Style="{StaticResource sheetNavigationButton}"
                Command="{x:Static ssui:WorkbookNavigationCommands.MoveToNext}" CommandTarget="{Binding ElementName=lb}">
          <Path Data="M0,0 L0,6 L3,3 Z" />
        </Button>
        <Button DockPanel.Dock="Left" Style="{StaticResource sheetNavigationButton}"
                Command="{x:Static ssui:WorkbookNavigationCommands.MoveToLast}" CommandTarget="{Binding ElementName=lb}">
          <Path Data="M0,0 L0,6 L3,3 Z M6,0 L6,6" />
        </Button>
        <DockPanel Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.SheetTabBorderBrushKey}}"
                   LastChildFill="False">
          <ListBox DockPanel.Dock="Left" Focusable="True" SelectedItem="{Binding View.ActiveSheet, Mode=TwoWay}"
                   Style="{StaticResource dragDropListBox}" ItemContainerStyle="{StaticResource sheetTab}" ItemsSource="{Binding Sheets}"
                   Name="lb" Margin="0 0 0 -2" Padding="0 0 0 0" BorderThickness="0" TextElement.FontSize="12"
                   Foreground="{DynamicResource {x:Static oui:OfficeBrushes.TextForegroundKey}}" Background="Red"
                   ScrollViewer.HorizontalScrollBarVisibility="Hidden" Panel.ZIndex="1">
            <ListBox.Template>
              <ControlTemplate TargetType="{x:Type ListBox}">
                <ScrollViewer>
                  <ItemsPresenter />
                </ScrollViewer>
              </ControlTemplate>
            </ListBox.Template>
            <ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                <Internals:ReverseZIndexStackPanel Orientation="Horizontal" />
              </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
          </ListBox>
          <Button DockPanel.Dock="Left" Style="{StaticResource sheetButton}" Margin="0 1 0 0"
                  Command="{x:Static ssui:SpreadsheetCommands.InsertWorksheet}" ToolTip="Insert Worksheet">
            <Image Source="SheetNew.png" Height="12" Width="12" Margin="12 0 0 0" />
          </Button>
        </DockPanel>
      </DockPanel>

      <GridSplitter Grid.Column="1" Grid.Row="1" ResizeBehavior="PreviousAndNext" Width="4" Background="LightGray"
                    Name="splitter0" Focusable="False" />
      <ScrollBar Name="verticalScrollBar" Style="{StaticResource officeScrollBar}" Grid.Column="3" Grid.Row="0"
                 Value="{Binding Path=View.ActiveSheet.ScrollPane.Top}" ButtonBase.Click="OnScrollBarButtonClick"
                 ViewportSize="{Binding Path=View.ActiveSheet.ScrollPane.ViewportHeight}"
                 Minimum="{Binding RelativeSource={RelativeSource Self}, Path=(ssui:SpreadsheetElement.WorksheetViewModel).MinRow }"
                 Maximum="{Binding RelativeSource={RelativeSource Self}, Path=(ssui:SpreadsheetElement.WorksheetViewModel).MaxRowPosition }"
                 SmallChange="1" LargeChange="{Binding Path=View.ActiveSheet.ScrollPane.RowCount}" Orientation="Vertical" Thumb.DragStarted="onDeferScrollUpdate" Thumb.DragCompleted="onDoScrollUpdate"/>
      <ScrollBar Style="{StaticResource officeScrollBar}" Grid.Row="1" Grid.Column="2" Name="horizontalScrollBar"
                 Value="{Binding Path=View.ActiveSheet.ScrollPane.Left}" ButtonBase.Click="OnScrollBarButtonClick"
                 Minimum="{Binding RelativeSource={RelativeSource Self}, Path=(ssui:SpreadsheetElement.WorksheetViewModel).MinColumn}"
                 Maximum="{Binding RelativeSource={RelativeSource Self}, Path=(ssui:SpreadsheetElement.WorksheetViewModel).MaxColumnPosition}"
                 SmallChange="1" LargeChange="{Binding Path=View.ActiveSheet.ScrollPane.ColumnCount}"
                 ViewportSize="{Binding Path=View.ActiveSheet.ScrollPane.ViewportWidth}" Orientation="Horizontal" 
                 Thumb.DragStarted="onDeferScrollUpdate" Thumb.DragCompleted="onDoScrollUpdate"/>
      <ContentControl Content="{Binding Path=View.ActiveSheet}" Grid.ColumnSpan="3" Name="activeSheet"
                      Internals:FocusElement.FocusDown="true" />
    </Grid>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding View.ShowSheetTabs}" Value="False">
        <Setter TargetName="navigationBox" Property="Visibility" Value="Collapsed"></Setter>
      </DataTrigger>
      <DataTrigger Binding="{Binding View.ShowVerticalScroll}" Value="False">
        <Setter TargetName="horizontalScrollBar" Property="Visibility" Value="Collapsed"></Setter>
      </DataTrigger>
      <DataTrigger Binding="{Binding View.ShowVerticalScroll}" Value="False">
        <Setter TargetName="verticalScrollBar" Property="Visibility" Value="Collapsed"></Setter>
      </DataTrigger>
    </DataTemplate.Triggers>

  </DataTemplate>


  <DataTemplate DataType="{x:Type ss:Workbook}">
    <Grid PreviewKeyDown="OnGridPreviewKeyDown" KeyDown="OnGridKeyDown" Name="selfGrid"
          Internals:EditionElement.EditorRoot="{Binding ElementName=selfGrid}" ssui:SpreadsheetElement.Workbook="{Binding}"
          Internals:EditionElement.WorkbookOwner="{Binding RelativeSource={RelativeSource Self}}">
      <Grid.RowDefinitions>
        <RowDefinition Height="20" MinHeight="20"
                       Internals:GridExtensions.IsCollapsed="{Binding View.ActiveSheet.View.FormulasVisible, Converter={x:Static Internals:SpreadsheetConverters.NotConverter}}" />
        <RowDefinition Height="Auto"
                       Internals:GridExtensions.IsCollapsed="{Binding View.ActiveSheet.View.FormulasVisible, Converter={x:Static Internals:SpreadsheetConverters.NotConverter}}" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <ContentControl Content="{Binding Path=View.ActiveSheet}" ContentTemplate="{StaticResource formulaBar}"
                      Name="formulaBar" Focusable="False" drawingui:DrawingElement.ColorScheme="{Binding Path=Theme.ColorScheme}" />
      <GridSplitter Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" ResizeDirection="Rows"
                    Height="5" Background="{DynamicResource {x:Static ss:SpreadsheetBrushes.FormulaBackgroundKey}}"
                    BorderBrush="{DynamicResource {x:Static ss:SpreadsheetBrushes.FormulaBorderBrushKey}}" BorderThickness="1"
                    ResizeBehavior="PreviousAndNext" Name="splitter2" Focusable="False" />

      <Grid Grid.Row="2" Name="fullworksheetGrid">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <ContentControl Grid.Column="0" Background="Gray" Content="{Binding}"
                        ContentTemplate="{StaticResource smallWorkbook}" ClipToBounds="True" Name="workbook" Focusable="False" />
        <GridSplitter Grid.Column="1" ResizeDirection="Columns" Name="splitter3" Focusable="False"
                      ResizeBehavior="PreviousAndCurrent" VerticalAlignment="Stretch" HorizontalAlignment="Left" />
        <ItemsControl Visibility="Collapsed" Background="LightBlue" Grid.Column="1" Name="anchorsControl"
                      ItemsSource="{Binding Path=View.ActiveSheet.Anchors}" />

      </Grid>

    </Grid>
  </DataTemplate>

</ResourceDictionary>